import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import(/* webpackChunkName: "about" */ '../views/home/HomeView.vue'),
    meta: {
      title: '网上商城'
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import(/* webpackChunkName: "about" */ '../views/category/CategoryView.vue'),
    meta: {
      title: '分类'
    }
  },
  {
    path: '/shopcart',
    name: 'Shopcart',
    component: () => import(/* webpackChunkName: "about" */ '../views/shopcart/ShopcartView.vue'),
    meta: {
      title: '购物车'
    }
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import(/* webpackChunkName: "about" */ '../views/detail/DetailView.vue'),
    meta: {
      title: '商品详情'
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import(/* webpackChunkName: "about" */ '../views/profile/ProfileView.vue'),
    meta: {
      title: '我的'
    }
  },
  {
    path: '/:catchAll(.*)', // 匹配所有路径
    name: 'page-404',
    component: () => import(/* webpackChunkName: "about" */ '../views/Page-404.vue'),
    meta: {
      title: '页面不存在'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/profile/LoginView.vue'),
    meta: {
      title: '用户登录'
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import(/* webpackChunkName: "about" */ '../views/profile/RegisterView.vue'),
    meta: {
      title: '新用户注册'
    }
  },
  {
    path: '/checkout',
    name: 'Checkout',
    component: () => import(/* webpackChunkName: "about" */ '../views/shopcart/CheckoutView.vue'),
    meta: {
      title: '订单确认'
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router
